package com.gip.service.Impl;

import com.gip.dto.ResgisterInfo;
import com.gip.mapper.UserMapper;
import com.gip.pojo.User;
import com.gip.pojo.UserExample;
import com.gip.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> isLogin(String username, String userPassword) {

        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andUserpasswordEqualTo(userPassword);
        List<User> userList = this.userMapper.selectByExample(example);
        return userList;
    }

    @Override
    public int registerUser(ResgisterInfo resgisterInfo) {
        User user = new User();
        try {
            user.setUsername(resgisterInfo.getUsername());
            user.setUserpassword(resgisterInfo.getUserpassword());
            user.setUsercardno(resgisterInfo.getUsercardno());
            user.setUsertel(resgisterInfo.getUsertel());
            user.setUseraddress(resgisterInfo.getUseraddress());
            user.setUserpostnumber(resgisterInfo.getUserpostnumber());
            user.setUserisadmin(0);
            //执行插入语句
            int insert = userMapper.insert(user);
            return insert;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateUser(String username, String tel, String password) {
        //根据条件进行查行
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andUsertelEqualTo(tel);
        List<User> users = userMapper.selectByExample(userExample);
        if (users != null && users.size() != 0) {
            User user = users.get(0);
            user.setUserpassword(password);
            int insert = userMapper.updateByPrimaryKey(user);
            if (insert >= 1) {
                return 1;
            } else {
                return 0;
            }
        }
        return 0;

    }
}
